<template>
  <div class="login-container">
    <div class="login-card">
      <!-- 登录标题 -->
      <h2 class="login-title">登录</h2>

      <!-- 登录表单 -->
      <a-form layout="vertical" class="login-form">
        <!-- Email 输入 -->
        <a-form-item label="Email">
          <a-input
            class="login-input"
            placeholder="请输入您的邮箱地址" 
            size="large"
            v-model:value="loginForm.email"
          />
        </a-form-item>
        
        <div class="password-container">
          <div class="password-header">
            <label for="passwordInput" class="password-label">密码</label>
            <a href="#" class="forgot-password" @click="goToFrogot">忘记密码？</a>
          </div>
          <a-input-password
            type="password"
            id="passwordInput"
            placeholder="请输入密码"
            v-model:value="loginForm.password"
          />
        </div>
        
        <!-- 登录按钮 -->
        <a-button 
          type="primary" 
          size="large" 
          class="login-btn"
          @click="handleLogin"
          :loading="loading"
          block
        >
          登录
        </a-button>
        
        <!-- 注册链接 -->
        <div class="register-link">
          没有账号?
          <a @click="goToRegister">在此注册</a>
        </div>
      </a-form>

      <!-- 第三方登录 -->
      <div class="social-login">
        <a-button 
          type="primary" 
          class="social-btn wechat-btn"
        >
          <Icon icon="fa6-brands:weixin" />
          &nbsp;微信登录
        </a-button>
      </div>
      <div>
        <a-button 
          type="primary" 
          class="social-btn weibo-btn"
        >
          <Icon icon="fa6-brands:weibo" />
          &nbsp;微博登录
        </a-button>
      </div>
    </div>
  </div>
</template>

<script>
import { Icon } from '@iconify/vue'

export default {
  name: 'LoginView',
  components: { Icon },
  data() {
    return {
      loginForm: {
        email: "test@qq.com",
        password: "test1234"
      },
      loading: false
    };
  },
  methods: {
    goToRegister() {
      // 这里添加导航到登录页的逻辑
      this.$router.push('/register');
    },
    goToFrogot() {
      // 这里添加导航到登录页的逻辑
      this.$router.push('/forgotpassword');
    },
    async handleLogin() {
      this.loading = true;
      try {
        // 注意：这里需要更新userApi以适应Ant Design Vue
        const response = await this.$userApi.login({
          username: this.loginForm.email,
          password: this.loginForm.password,
        });
        
        // 登录成功，保存token
        if (response && response.access_token) {
          localStorage.setItem('ACCESS_TOKEN', response.access_token);

          // 获取并保存用户信息
          try {
            const userResponse = await this.$userApi.getUserInfo();
            const userInfo = userResponse.data || userResponse;
            localStorage.setItem('USER_ID', userInfo.id);
            localStorage.setItem('USER_EMAIL', userInfo.email);
            localStorage.setItem('USERNAME', userInfo.name || userInfo.full_name);
          } catch (userError) {
            console.error('获取用户信息失败:', userError);
          }

          // 登录成功后处理Flarum集成
          // 从响应中获取FastAPI的token
          const fastApiToken = response.access_token
          
          // 构建Flarum认证URL
          const flarumAuthUrl = new URL("http://192.168.3.86:8100/fastapi-auth")
          flarumAuthUrl.searchParams.append("token", fastApiToken)
          flarumAuthUrl.searchParams.append("redirect", window.location.origin)
          
          // 跳转到Flarum进行认证
          window.location.href = flarumAuthUrl.toString()
        } else {
          this.$message.error('登录失败：无效的响应数据');
        }
      } catch (error) {
        console.error('登录失败:', error);
        this.$message.error('登录失败：' + (error.message || '未知错误'));
      } finally {
        this.loading = false;
      }
    }
  }
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #fff;
  padding: 20px;
}

.login-card {
  width: 100%;
  max-width: 420px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
  padding: 40px;
}

.login-title {
  text-align: center;
  font-size: 24px;
  font-weight: 600;
  color: #333;
  margin-bottom: 30px;
}

.login-form {
  margin-bottom: 30px;
}

.password-input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}

.login-btn {
  height: 44px;
  font-size: 16px;
  margin-top: 30px;
  font-weight: 500;
}

.register-link {
  display: flex; 
  align-items: center; 
  justify-content: center;
  gap: 8px;
  margin-top: 20px;
  font-size: 14px;
}

.social-login {
 margin-bottom: 20px;
}

.social-btn {
  width: 100%;
  height: 44px;
  font-size: 16px;
  font-weight: 500;
  display: flex;
  justify-content: center;
  align-items: center;
}

.wechat-btn {
  background-color: #07bb07;
  border-color: #07bb07;
  color: white;
}

.wechat-btn:hover {
  background-color: #06a306;
  border-color: #06a306;
}

.weibo-btn {
  background-color: #ff8200;
  border-color: #ff8200;
  color: white;
}

.weibo-btn:hover {
  background-color: #e67400;
  border-color: #e67400;
}

.social-icon {
  margin-right: 8px;
  font-size: 18px;
}

.password-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.password-label {
  font-size: 14px;
  color: #000;
  font-weight: 500;
}

.forgot-password {
  font-size: 14px;
  color: #1f50f3;
  text-decoration: none;
  transition: color 0.3s;
}

.forgot-password:hover {
  color: #1890ff;
}
.login-input{
  padding: 4px 11px;
}
.password-input {
  width: 100%;
  height: 40px;
  padding: 8px 12px;
  border: 1px solid #1890ff;
  border-radius: 4px;
  font-size: 14px;
  outline: none;
  box-sizing: border-box;
  background: #fff;
  transition: border-color 0.3s;
}

.password-input:focus {
  border-color: #40a9ff;
  box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.2);
}

.password-input::placeholder {
  color: #bfbfbf;
}
</style>